// // hello.servo.44.c // // servo motor hello-world // // set lfuse to 0x7E for 20 MHz xtal // // Neil Gershenfeld // 4/8/12 // // (c) Massachusetts Institute of Technology 2012 // Permission granted for experimental and personal use; // license for commercial sale available from MIT. // #include #include #define output(directions,pin) (directions |= pin) // set port direction for output #define set(port,pin) (port |= pin) // set port pin #define clear(port,pin) (port &= (~pin)) // clear port pin #define pin_test(pins,pin) (pins & pin) // test for port pin #define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set #define position_delay() _delay_ms(500) #define PWM_port PORTA #define PWM_pin (1 << PA6) #define PWM_direction DDRA int main(void) { // // main // // set clock divider to /1 // CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); // // set up timer 1 // TCCR1A = (1 << COM1A1) | (0 << COM1A0); // clear OC1A on compare match TCCR1B = (0 << CS12) | (1 << CS11) | (0 << CS10) | (1 << WGM13); // prescaler /8, phase and frequency correct PWM, ICR1 TOP ICR1 = 25000; // 20 ms frequency // // set PWM pin to output // clear(PWM_port, PWM_pin); output(PWM_direction, PWM_pin); // // main loop // while (1) { // // 1 ms PWM on time // OCR1A = 500; position_delay(); // // 1.5 ms PWM on time // OCR1A = 1000; position_delay(); // // 2 ms PWM on time // OCR1A = 1200; position_delay(); } }